package com.chao.wind3j.base.config.shiroConfig;

import com.chao.wind3j.base.config.commonConfig.CommonProperties;
import com.chao.wind3j.base.config.commonConfig.requestLogConfig.requestWrapper.CustomRequestWrapper;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;

public class MySessionManager extends DefaultWebSessionManager {
  private final CommonProperties commonProperties;

  public MySessionManager(CommonProperties commonProperties) {
    this.commonProperties = commonProperties;
  }

  @Override
  protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
    CustomRequestWrapper customRequestWrapper = new CustomRequestWrapper(WebUtils.toHttp(request), this.commonProperties);
    var token = customRequestWrapper.getRequestInfo().getAuthToken().getToken();
    if(token!= null) {
      return token;
    } else {
      return super.getSessionId(request, response);
    }
  }
}
